package com.yonyou.crm.sprc.oppstage.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sprc.oppstage.entity.OppstageVO;
import com.yonyou.crm.sprc.oppstage.rmitf.IOppstageRmService;

@RestController
@RequestMapping(value = "/sprc/oppstages")
public class OppstageCtrl {

	@Autowired
	private IOppstageRmService service;
	
	/**
	 * 商机阶段列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);

		@SuppressWarnings("unchecked")
		List<OppstageVO> voList = service.list((Map<String, Object>)paramObj.get("searchMap"));
		
		return ResponseUtil.getObject(getListData(voList));
		
	}

	/**
	 * 获取全部商机阶段
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "all",method = RequestMethod.GET)
	public Object getAll(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);

		@SuppressWarnings("unchecked")
		List<OppstageVO> voList = service.list(new HashMap());

		return ResponseUtil.getObject(getListData(voList));

	}


	
	/**
	 * 新增商机阶段
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		OppstageVO vo = RequestUtil.getVO(request, OppstageVO.class);
		
		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		
		vo = service.insert(vo);
		
		if (vo.getId() == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "获取自增主键为空");
		}
				
		return ResponseUtil.getObject(vo);
	}
	
	/**
	 * 修改商机阶段
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		if (id == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "主键不能为空");
		}
		
		OppstageVO vo = RequestUtil.getVO(request, OppstageVO.class);
		if (vo == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "转换实体失败");
		}
		
		vo.setId(id);
		
		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		
		return ResponseUtil.getObject(service.update(vo));
	}
	
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");		

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}
		
		String[] idArray = idsStr.split(",");
		
		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}
		
		@SuppressWarnings("unchecked")
		List<OppstageVO> voList = service.batchDelete(idArray, (Map<String, Object>)paramObj.get("searchMap"));
				
		return ResponseUtil.getObject(getListData(voList));
	}
	
	/**
	 * 批量停启用
	 * @param request
	 * @param response
	 * @return
	 */
	
	@RequestMapping(value = "state", method = RequestMethod.PUT)
	public Object batchUpdateEnableState(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");		

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}
		
		String[] idArray = idsStr.split(",");
		
		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}
		
		Integer enableState = paramObj.getInt("enableState");
		
		if (enableState == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}
		
		@SuppressWarnings("unchecked")
		List<OppstageVO> voList = service.batchUpdateEnableState(idArray, enableState, (Map<String, Object>)paramObj.get("searchMap"));
				
		return ResponseUtil.getObject(getListData(voList));
	}
	
	private Map<String, Object> getListData(List<OppstageVO> voList) {
		
		if (voList == null) {
			voList = new ArrayList<OppstageVO>();
		}
		
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("voList", voList);
		
		return data;
	}
	
	private String validator(OppstageVO vo) {
		
		if (vo == null) {
			return "转换实体失败";
		}
		
		if (StringUtils.isBlank(vo.getName())) {
        	return  "名称不能为空";
		}

		return "";
	}

	/**
	 * 根据商机流程商机阶段
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "/biztype",method = RequestMethod.GET)
	public Object getStageByFlow(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		String biztype = paramObj.getString("biztype");
		@SuppressWarnings("unchecked")
		List<OppstageVO> voList = service.getStageByBiztype(biztype);
		List<Map<String,Object>> stageEnum = new ArrayList<Map<String,Object>>();
		for(OppstageVO vo: voList){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("key",vo.getId());
			map.put("title",vo.getName());
			stageEnum.add(map);
		}
		JSONObject result = new JSONObject();
		result.put("stageEnum",stageEnum);
		return ResponseUtil.getObject(result);

	}

}
